﻿// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved

using RWSBeeldbank.Common;
using RWSBeeldbank.Data;
using System;
using System.Threading.Tasks;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;

namespace RWSBeeldbank
{
    partial class ExtendedSplash
    {
        internal Rect splashImageRect; // Rect to store splash screen image coordinates.
        internal bool dismissed = false; // Variable to track splash screen dismissal status.
        internal Frame rootFrame;

        private SplashScreen splash; // Variable to hold the splash screen object.

        public ExtendedSplash(SplashScreen splashscreen, bool loadState)
        {
            InitializeComponent();

            // Listen for window resize events to reposition the extended splash screen image accordingly.
            // This is important to ensure that the extended splash screen is formatted properly in response to snapping, unsnapping, rotation, etc...
            Window.Current.SizeChanged += new WindowSizeChangedEventHandler(ExtendedSplash_OnResize);

            splash = splashscreen;

            if (splash != null)
            {
                // Register an event handler to be executed when the splash screen has been dismissed.
                splash.Dismissed += new TypedEventHandler<SplashScreen, Object>(DismissedEventHandler);

                // Retrieve the window coordinates of the splash screen image.
                splashImageRect = splash.ImageLocation;
                PositionImage();
            }

            // Create a Frame to act as the navigation context 
            rootFrame = new Frame();
            
            // Restore the saved session state if necessary
            RestoreStateAsync(loadState);            
        }

        async void RestoreStateAsync(bool loadState)
        {
            if (loadState)
                await SuspensionManager.RestoreAsync();
            
            // Normally you should start the time consuming task asynchronously here and 
            // dismiss the extended splash screen in the completed handler of that task
            // This sample dismisses extended splash screen  in the handler for "Learn More" button for demonstration
        }

        // Position the extended splash screen image in the same location as the system splash screen image.
        void PositionImage()
        {
            extendedSplashImage.SetValue(Canvas.LeftProperty, splashImageRect.X);
            extendedSplashImage.SetValue(Canvas.TopProperty, splashImageRect.Y);
            extendedSplashImage.Height = splashImageRect.Height;
            extendedSplashImage.Width = splashImageRect.Width;

            // Instantiate random number generator using system-supplied value as seed.
            Random rand = new Random();

            Uri _baseUri = new Uri("ms-appx:///");

            // Set random image source
            SplashImage1.Source = new BitmapImage(new Uri(_baseUri, "Images/" + rand.Next(1, 8) + ".jpg"));
            SplashImage2.Source = new BitmapImage(new Uri(_baseUri, "Images/" + rand.Next(9, 16) + ".jpg"));
            SplashImage3.Source = new BitmapImage(new Uri(_baseUri, "Images/" + rand.Next(17, 24) + ".jpg"));
            SplashImage4.Source = new BitmapImage(new Uri(_baseUri, "Images/" + rand.Next(25, 32) + ".jpg"));
            SplashImage5.Source = new BitmapImage(new Uri(_baseUri, "Images/" + rand.Next(33, 40) + ".jpg"));
            SplashImage6.Source = new BitmapImage(new Uri(_baseUri, "Images/" + rand.Next(41, 48) + ".jpg"));
            int randomValue = rand.Next(49, 56);
            if (randomValue == 53) randomValue = 57;
            if (randomValue == 54) randomValue = 58;
            SplashImage7.Source = new BitmapImage(new Uri(_baseUri, "Images/" + randomValue + ".jpg"));

            // Set Horizontal position with random value
            img1.SetValue(Canvas.LeftProperty, splashImageRect.X - 200);
            img2.SetValue(Canvas.LeftProperty, splashImageRect.X - 75);
            img3.SetValue(Canvas.LeftProperty, splashImageRect.X + 50);
            img4.SetValue(Canvas.LeftProperty, splashImageRect.X + 175);
            img5.SetValue(Canvas.LeftProperty, splashImageRect.X + 300);
            img6.SetValue(Canvas.LeftProperty, splashImageRect.X + 425);
            img7.SetValue(Canvas.LeftProperty, splashImageRect.X + 550);
            // Set Vertical position with random value
            img1.SetValue(Canvas.TopProperty, splashImageRect.Y - 100 - rand.Next(50));
            img2.SetValue(Canvas.TopProperty, splashImageRect.Y - 100 + rand.Next(50));
            img3.SetValue(Canvas.TopProperty, splashImageRect.Y - 100 - rand.Next(50));
            img4.SetValue(Canvas.TopProperty, splashImageRect.Y - 100 + rand.Next(50));
            img5.SetValue(Canvas.TopProperty, splashImageRect.Y - 100 - rand.Next(50));
            img6.SetValue(Canvas.TopProperty, splashImageRect.Y - 100 + rand.Next(50));
            img7.SetValue(Canvas.TopProperty, splashImageRect.Y - 100 - rand.Next(50));

            // Associate the transforms to the button.
            img1.RenderTransform = CreateTransformation(rand.Next(50) - 25);
            img2.RenderTransform = CreateTransformation(rand.Next(50) - 25);
            img3.RenderTransform = CreateTransformation(rand.Next(50) - 25);
            img4.RenderTransform = CreateTransformation(rand.Next(50) - 25);
            img5.RenderTransform = CreateTransformation(rand.Next(50) - 25);
            img6.RenderTransform = CreateTransformation(rand.Next(50) - 25);
            img7.RenderTransform = CreateTransformation(rand.Next(50) - 25);
        }

        private static TransformGroup CreateTransformation(int rand)
        {
            // Create a transform to rotate the button
            RotateTransform myRotateTransform = new RotateTransform();
            // Set the rotation of the transform to 45 degrees.
            myRotateTransform.Angle = rand;
            // Create a TransformGroup to contain the transforms 
            // and add the transforms to it.
            TransformGroup myTransformGroup = new TransformGroup();
            myTransformGroup.Children.Add(myRotateTransform);
            return myTransformGroup;
        }

        void ExtendedSplash_OnResize(Object sender, WindowSizeChangedEventArgs e)
        {
            // Safely update the extended splash screen image coordinates. This function will be fired in response to snapping, unsnapping, rotation, etc...
            if (splash != null)
            {
                // Update the coordinates of the splash screen image.
                splashImageRect = splash.ImageLocation;
                PositionImage();
            }
        }

        // Include code to be executed when the system has transitioned from the splash screen to the extended splash screen (application's first view).
        void DismissedEventHandler(SplashScreen sender, object e)
        {
            dismissed = true;

            // Navigate away from the app's extended splash screen after completing setup operations here...
            // This sample navigates away from the extended splash screen when the "Learn More" button is clicked.
        }
    }
}
